# Makes three unknown figures?
# libs --------------------------------------------------------------------
library(here)
library(tidyverse)
library(haven)

# data --------------------------------------------------------------------
data <- read_dta(file = here('data/modified data/analysis.dta'))

ca <- data %>% 
  filter((year%%4)==2 & prim == 0 & state == 'CA') %>% 
  group_by(vca18, year) %>% 
  summarize(ts = mean(turnout_share))

ut <- data %>% 
  filter((year%%4)==0 & prim == 0 & state == 'UT') %>% 
  rowwise() %>% 
  mutate(ut_switch_btwn_12_16 = ut_all_mail_year %in% c(2014,2016)) %>% 
  ungroup() %>% 
  filter(ut_all_mail_year >=  2014) %>% 
  group_by(ut_switch_btwn_12_16, year) %>% 
  summarize(ts = mean(turnout_share))

wa <- data %>% 
  filter((year%%4)==0 & prim == 0 & state == 'WA', 
         switch_year %in% c(0, 2006, 2010)) %>% 
  group_by(all_mail2006, year) %>% 
  summarize(ts = mean(turnout_share, na.rm=T))



# plots -------------------------------------------------------------------
# what are these labels????
ca %>% ggplot(aes(x = year, y = ts, color = as.character(vca18))) +
  geom_point() +
  geom_line() +
  labs(x = 'Year', y = 'Turnout / Voting-Age Pop', title = 'California') + 
  lims(x = c(1997, 2019)) + 
  annotate(geom = 'text', y = .43, x = 2003, label = 'Treated Counties') +
  annotate(geom = 'text', y = .378, x = 2007, label = 'Control Counties') +
  theme_bw() +
  theme(legend.position = 'none')

ut %>% ggplot(aes(x = year, y = ts, color = ut_switch_btwn_12_16)) +
  geom_point() +
  geom_line() +
  labs(x = 'Year', y = 'Turnout / Voting-Age Pop', title = 'Utah') +  
  lims(x = c(1995, 2017)) + 
  annotate(geom = 'text', y = .576, x = 2003, label = 'Treated Counties') +
  annotate(geom = 'text', y = .627, x = 1998, label = 'Control Counties') +
  theme_bw() +
  theme(legend.position = 'none')

wa %>% ggplot(aes(x = year, y = ts, color = as.character(all_mail2006))) +
  geom_point() +
  geom_line() +
  labs(x = 'Year', y = 'Turnout / Voting-Age Pop', title = 'Washington') +  
  lims(x = c(1995, 2009)) + 
  annotate(geom = 'text', y = .593, x = 1998, label = 'Treated Counties') +
  annotate(geom = 'text', y = .575, x = 2003, label = 'Control Counties') +
  theme_bw() +
  theme(legend.position = 'none')
